<script lang="ts" setup name="XtxConfirm">
import { PropType, ref, onMounted } from 'vue'
import XtxButton from '@/components/button/index.vue'
defineProps({
  title: {
    type: String,
    default: '默认标题'
  },
  text: {
    type: String,
    default: '默认内容'
  },
  cancelCallback: {
    type: Function as PropType<() => void>
  },
  confirmCallback: {
    type: Function as PropType<() => void>
  }
})
const flag = ref(false)
onMounted(() => {
  // onMounted是dom挂载, 等到DOM挂载后 再开一个宏任务 开启 flag
  // 宏任务(主代码块 定时器 延时器) 微任务 .then() 
  // 加载的时候会先执行主代码块的宏任务1 等第一个宏任务执行完毕后, 会看是否与微任务,有就执行微任务
  // 微任务执行完 ,再继续执行下一个宏任务
  setTimeout(() => {
    flag.value = true
  }, 0)
})
</script>
<template>
  <!-- Transition实现的动画 整体控制 -->
  <!-- 
    <Transition name="down">
    <div class="xtx-confirm" @click.self="cancelCallback" v-if="flag">
      <div class="wrapper">
        <div class="header">
          <h3>{{ title }}</h3>
          <a href="JavaScript:;" class="iconfont icon-close-new" @click="cancelCallback"></a>
        </div>
        <div class="body">
          <i class="iconfont icon-warning"></i>
          <span>{{ text }}</span>
        </div>
        <div class="footer">
          <XtxButton size="mini" type="gray" @click="cancelCallback">取消</XtxButton>
          <XtxButton size="mini" type="primary" @click="confirmCallback">确认</XtxButton>
        </div>
      </div>
    </div>
  </Transition> -->
  <!-- 控制两个元素的动画  一个是 外层 遮罩的样式 淡入  
       wrapper 从上方掉下来
       默认先让元素看不见 ==>>>  类名的展示看见
  -->
  <div class="xtx-confirm" @click.self="cancelCallback" :class="{ fade: flag }">
    <div class="wrapper" :class="{ fade: flag }">
      <div class="header">
        <h3>{{ title }}</h3>
        <a href="JavaScript:;" class="iconfont icon-close-new" @click="cancelCallback"></a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{ text }}</span>
      </div>
      <div class="footer">
        <XtxButton size="mini" type="gray" @click="cancelCallback">取消</XtxButton>
        <XtxButton size="mini" type="primary" @click="confirmCallback">确认</XtxButton>
      </div>
    </div>
  </div>

</template>

<style scoped lang="less">
.xtx-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0);

  &.fade {
    transition: all .3s;
    background: rgba(0, 0, 0, .5);
  }

  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -100%);
    opacity: 0;

    &.fade {
      transition: all .3s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }

    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }

    .body {
      padding: 20px 40px;
      font-size: 16px;

      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }

    .footer {
      text-align: right;

      .xtx-button {
        margin-left: 20px;
      }
    }

    .header {
      position: relative;

      h3 {
        font-weight: normal;
        font-size: 18px;
      }

      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;

        &:hover {
          color: #666;
        }
      }
    }
  }
}

.down-enter-from {
  opacity: 0;
}

.down-enter-active {
  transition: all .5s;
}

.down-enter-to {
  opacity: 1;
}
</style>